
package com.example.curvedemo.util;

import android.app.Activity;
import android.util.DisplayMetrics;

public class ScreenUtil {


    public static int dip2px(int i) {
        return (int) (0.5D + (double) (getDensity() * (float) i));
    }

    public static int sp2px(float spValue) {
        float fontScale = AppContextUtil.getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(float pxValue) {
        final float scale = getDensity();
        return (int) (pxValue / scale + 0.5f);
    }

    public static float getDensity() {
        return AppContextUtil.getContext().getResources().getDisplayMetrics().density;
    }

    public static float getXDpi() {
        return AppContextUtil.getContext().getResources().getDisplayMetrics().xdpi;
    }

    public static float getYDpi() {
        return AppContextUtil.getContext().getResources().getDisplayMetrics().ydpi;
    }

    public static float getDpi() {
        return AppContextUtil.getContext().getResources().getDisplayMetrics().densityDpi;
    }

    public static int getScreenWidth() {
        return AppContextUtil.getContext().getResources().getDisplayMetrics().widthPixels;
    }

    public static int getScreenHeight() {
        return AppContextUtil.getContext().getResources().getDisplayMetrics().heightPixels;
    }

    public static int getScreenHeight(Activity context) {
        DisplayMetrics metrics = new DisplayMetrics();
        context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.heightPixels;
    }


}
